package Serveur;

import java.util.ArrayList;
import java.util.Map;

public class SparqlQueries {

	private SparqlClient sparql;
	public SparqlQueries()
	{
		sparql = new SparqlClient("127.0.0.1:3030/space");

        String query = "ASK WHERE { ?s ?p ?o }";
        boolean serverIsUp = sparql.ask(query);
        if (serverIsUp) {
            System.out.println("server is UP");
        } else {
            System.out.println("service is DOWN");
        }		
	}
 
    public ArrayList<String> getSynonymes(String mot) {
    	
    	ArrayList<String> synonymes = new ArrayList<String>();
        String query = "PREFIX : <http://ontologies.alwaysdata.net/space#>\n"
        			+ "PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
        			+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"
        			+ "PREFIX owl:  <http://www.w3.org/2002/07/owl#>\n"
        			+ "PREFIX xsd:  <http://www.w3.org/2001/XMLSchema#>\n"
        			
                    + "SELECT ?subject ?synonymes WHERE\n"
                    + "{\n"
                    + "     ?subject rdfs:label ?label.\n"
                    + "     FILTER (regex(?label, '^"+mot+"$','i')).\n"
                    + "     ?subject rdfs:label ?synonymes.\n"
                    + "     FILTER (?synonymes != ?label).\n"              
                    + "}\n";
            Iterable<Map<String, String>> results = sparql.select(query);
            System.out.println("Synonyme de "+mot+":");
            for (Map<String, String> result : results) {
            	synonymes.add(result.get("synonymes"));
            }
            return synonymes;
    }  
}
